﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

using XFSale.Lib;
using DevExpress.XtraNavBar;

namespace XFSale.UI
{
    using MenuMetaSet = List<MenuMeta>;
    using System.Reflection;
    using DevExpress.XtraEditors;

    public partial class WinMain : Form
    {
        protected static MenuMetaSet menuMeta = App.MenuMeta;

        public WinMain()
        {
            InitializeComponent();


            int i = 0;

            NavBarControl bar = null;
            NavBarGroup group = null;
            NavBarItem item = null;
            foreach (MenuMeta menu in menuMeta)
            {
                //初始化一级菜单
                if (menu.menuID.Length == 2)
                {
                    //新建PictureBox
                    /*PictureBox pic = new PictureBox();
                    pic.Image = System.Drawing.Image.FromFile("Image/" + menu.menuCaption + ".bmp");
                    pic.Name = menu.menuID;
                    pic.Width = 50;
                    pic.Height = 50;
                    pic.Top = 55 * i + 3;
                    pic.Left = (NavPanel.Width - pic.Width) / 2;
                    pic.SizeMode = PictureBoxSizeMode.CenterImage;
                    //pic.BorderStyle = BorderStyle.FixedSingle;
                    NavPanel.Controls.Add(pic);*/

                    i++;

                    //新建NavBar
                    bar = new NavBarControl();
                    bar.Name = menu.menuID;
                    bar.Text = menu.menuCaption;
                    bar.Dock = DockStyle.Fill;
                    bar.Visible = (i == 1);
                    bar.LargeImages = ImgBar;
                    bar.SmallImages = ImgBar2;
                    Splitter.Panel1.Controls.Add(bar);
                }

                //初始化二级菜单
                if (menu.menuID.Length == 4)
                {
                    //新建NavBarGroup
                    group = new NavBarGroup();
                    group.Name = menu.menuID;
                    group.Caption = menu.menuCaption;
                    group.GroupStyle = NavBarGroupStyle.SmallIconsList;
                    group.Expanded = true;
                    group.LargeImageIndex = 0;
                    group.SmallImageIndex = -1;
                    bar.Groups.Add(group);
                }

                //初始化三级菜单
                if (menu.menuID.Length == 6)
                {
                    //新建NavBarGroup
                    item = new NavBarItem();
                    item.Name = menu.menuID;
                    item.Caption = menu.menuCaption;
                    item.LargeImageIndex = -1;
                    item.SmallImageIndex = 0;
                    item.Tag = menu;
                    item.LinkClicked += new NavBarLinkEventHandler(BarItem_LinkClicked);
                    group.ItemLinks.Add(item);

                    if (string.IsNullOrEmpty(menu.callFunc))
                    {
                        item.Enabled = false;
                    }
                }
            }

            LblRegionCode.Caption = App.Global.RegionCode;
            LblBranchCode.Caption = App.Global.BranchCode;
            LblOpID.Caption = App.Global.OpID + " " + App.Global.OpName;
            LblSysStatus.Caption = App.Global.SysStatus;
            LblSysWorkDay.Caption = App.Global.SysWorkDay;
            LblSysVersion.Caption = App.Global.SysVersion;
        }

        void BarItem_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            MenuMeta menu = (sender as NavBarItem).Tag as MenuMeta;
            if (string.IsNullOrEmpty(menu.callFunc))
                return;

            try
            {
                //Splitter.Panel1.Enabled = false;
                if (Splitter.Panel2.Controls.Count > 0)
                    return;

                /*foreach (Control ctrl in Splitter.Panel2.Controls)
                {
                    ctrl.Dispose();
                }
                Splitter.Panel2.Controls.Clear();*/
                /*NavBarControl NavBar = (Splitter.Panel1.Controls[0] as NavBarControl);
                foreach(NavBarGroup group in NavBar.Groups)
                {
                    foreach(NavBarItemLink item in group.VisibleItemLinks)
                    {
                        item.Item.Enabled = false;
                    }
                }*/

                string dll = null;
                if (!string.IsNullOrEmpty(menu.callDLL))
                    dll = menu.callDLL;
                FrmBase frame = Activator.CreateInstance(dll, menu.callFunc).Unwrap() as FrmBase;
                frame.Name = menu.menuID;
                frame.Tag = menu.callParam;
                frame.Dock = DockStyle.Left;
                Splitter.Panel2.Controls.Add(frame);
                
                //NavPanel.Enabled = false;
            }
            catch
            {
                SysMessage.Error("载入窗体失败");
            }
        }

        private void WinMain_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                //Splitter.Panel2.ContentImageAlignement = System.Drawing.ContentAlignment.TopLeft;
                //Splitter.Panel2.ContentImage = System.Drawing.Image.FromFile("Image/背景.gif");

                //注册快捷键
                bool success = RegisterHotKey(Handle, 100, KeyModifiers.Alt, Keys.Oemtilde);
                if (!success)
                {
                    SysMessage.Error("注册快捷键出错");
                }
            }
        }

        private void WinMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!this.DesignMode)
            {
                //取消快捷键
                bool success = UnregisterHotKey(Handle, 100);
                if (!success)
                {
                    SysMessage.Error("取消快捷键出错");
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;

            switch (m.Msg)
            {
                case WM_HOTKEY:
                    if (ActiveForm == this)
                    {
                        WinKey winKey = new WinKey();
                        winKey.ShowDialog(this);
                    }
                    break;
            }
            base.WndProc(ref m);
        }

        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,                // handle to window    
            int id,                     // hot key identifier    
            KeyModifiers fsModifiers,   // key-modifier options    
            Keys vk                     // virtual-key code    
        );

        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                // handle to window    
            int id                      // hot key identifier    
        );

        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        }
    }
}
